#!/bin/bash
if [[ ${#} == 0 ]];
    then
        printf "————请输入正确的指令！\n";
        printf "详情请看具体的帮助文档“-h” \n";
elif [[ $1 == -h ]]
    then
        printf "$0 (compile | upload | run | all) \n"
        printf "compile: 编译项目（包括子项目）\n"
        printf "upload: 上传jar包，需要配置免密！！！（不包括eureka-server,需要单独上传）\n"
        printf "run: 运行所有上传到目录里的jar\n"
        printf "all: 包括以上三个操作\n"
fi
# 项目根目录
wechet_root_dir="/Users/jifujun/java_workspaces/wechat/"
# 子项目目录名
wechet_sub_project_dir_arrays=(
    "exception"
    "common"
    "cors"
    "eureka-server"
    "fastdfs"
    "interceptor"
    "model"
    "persistence"
    "redis"
    "sso"
    "swagger"
    "aliyun-sms-interface"
    "dictionary-operate-interface"
    "dictionary-search-interface"
    "feedback-operate-interface"
    "feedback-search-interface"
    "manager-operate-interface"
    "manager-search-interface"
    "mistake-collection-operate-interface"
    "mistake-collection-search-interface"
    "question-collection-operate-interface"
    "question-collection-search-interface"
    "question-operate-interface"
    "question-search-interface"
    "ranking-operate-interface"
    "ranking-search-interface"
    "resource-study-operate-interface"
    "resource-study-search-interface"
    "staff-operate-interface"
    "staff-search-interface"
    "wechat-api-interface"
    "aliyun-sms-provider"
    "aliyun-sms-consumer"
    "dictionary-operate-provider"
    "dictionary-operate-consumer"
    "dictionary-search-provider"
    "dictionary-search-consumer"
    "feedback-operate-provider"
    "feedback-operate-consumer"
    "feedback-search-provider"
    "feedback-search-consumer"
    "manager-operate-provider"
    "manager-operate-consumer"
    "manager-search-provider"
    "manager-search-consumer"
    "mistake-collection-operate-provider"
    "mistake-collection-operate-consumer"
    "mistake-collection-search-provider"
    "mistake-collection-search-consumer"
    "question-collection-operate-provider"
    "question-collection-operate-consumer"
    "question-collection-search-provider"
    "question-collection-search-consumer"
    "question-operate-provider"
    "question-operate-consumer"
    "question-search-provider"
    "question-search-consumer"
    "ranking-operate-provider"
    "ranking-operate-consumer"
    "ranking-search-provider"
    "ranking-search-consumer"
    "resource-study-operate-provider"
    "resource-study-operate-consumer"
    "resource-study-search-provider"
    "resource-study-search-consumer"
    "staff-operate-provider"
    "staff-operate-consumer"
    "staff-search-provider"
    "staff-search-consumer"
    "wechat-api-provider"
    "wechat-api-consumer"
    )
    current_path=$(pwd)

arrays_length=${#wechet_sub_project_dir_arrays[*]}

function compile(){
    for i in $(seq 0 ${arrays_length});
        do
            if [[ $i -eq 0 ]];
                then
                    cd $wechet_root_dir
                    mvn install
            fi
        cd ${wechet_root_dir}${wechet_sub_project_dir_arrays[$i]}
        mvn install -DskipTests
    done;
    cd $current_path
}

function upload(){
    for i in $(seq 0 ${arrays_length});do
        if [[ ${wechet_sub_project_dir_arrays[$i]} == *-provider ]] || [[ ${wechet_sub_project_dir_arrays[$i]} == *-consumer ]]; then
            cd ${wechet_root_dir}${wechet_sub_project_dir_arrays[$i]}
            mvn package docker:build
        fi
    done;
}

# 编译整个项目(包括子项目)
if [[ $1 == compile ]];then
    compile
fi

# 上传所有jar包
if [[ $1 == upload ]];then
    printf "需要先配置免密！！！\n"
    upload
fi

# 上传所有jar包
if [[ $1 == run ]];then
    printf "需要先配置免密！！！\n"
    upload
fi

# 编译打包上传
if [[ $1 == all ]];then
    compile
    upload
fi